//go:build ignore

//自动编译
package main

//该源码思路没理清，代码较乱，该build之充当一个辅助编译的作用，将就着看吧，有时间再重写
//编译vue项目和go项目，复制到一起，组装静态文件

import (
	"fmt"
	"io"
	"os"
	"os/exec"
	"path/filepath"
	"stforum/sthelp"
	"time"

	"github.com/fatih/color"
)

func copyFile(a string, b string) {
	b_the, _ := os.Create(b)
	the, _ := os.Open(a)
	io.Copy(b_the, the)
	b_the.Close()
}

func main() {
	color.Yellow("ST论坛-Simple To Build & Simple To Use")
	color.Yellow("自动编译程序 powered by FCSQ-丁丁")
	color.Blue("出现问题请加入QQ群|点击链接加入群聊【ST论坛开发交流群】：https://jq.qq.com/?_wv=1027&k=7m4pnyGM")
	color.Blue("群号：1029518771")
	color.Blue("开源地址：https://gitee.com/fcsq-security-team2/st-forum")
	color.Blue("欢迎star支持！")
	color.Green("将在五秒后开始编译...")
	time.Sleep(5 * time.Second)
	os_dir, _ := os.Getwd()
	bin_dir, _ := filepath.Abs("bin")
	if sthelp.PathExists(bin_dir) {
		os.RemoveAll(bin_dir)
	}
	if sthelp.PathExists(filepath.Join(os_dir, "data")) {
		os.RemoveAll(filepath.Join(os_dir, "data"))
	}
	if sthelp.PathExists(filepath.Join(os_dir, "st.lock")) {
		os.RemoveAll(filepath.Join(os_dir, "st.lock"))
	}
	if !sthelp.PathExists(filepath.Join(os_dir, "data")) {
		os.Mkdir(filepath.Join(os_dir, "data"), 0777)
	}
	if !sthelp.PathExists(filepath.Join(bin_dir, "data", "static")) {
		os.Mkdir(filepath.Join(bin_dir, "data", "static"), 0777)
	}
	os.Mkdir(bin_dir, 0777)
	data_dir := filepath.Join(bin_dir, "data")
	os.Mkdir(data_dir, 0777)
	static_dir := filepath.Join(data_dir, "static")
	fmt.Println(static_dir)
	os.Mkdir(static_dir, 0777)
	admin_dir := filepath.Join(data_dir, "admin")
	os.Mkdir(admin_dir, 0777)
	index_dir := filepath.Join(data_dir, "index")
	fmt.Println("开始编译...")
	var vueprojects []string
	vueprojects = append(vueprojects, "initsite")
	//循环编译vue项目
	vue_path, _ := filepath.Abs("vue-project")
	for _, i := range vueprojects {
		build_path := filepath.Join(vue_path, i)
		com2 := exec.Command("npm", "i")
		com2.Dir = build_path
		com2.Stdout = os.Stdout
		com2.Stdin = os.Stdin
		com2.Stderr = os.Stderr
		com2.Run()
		com := exec.Command("npm", "run", "build")
		com.Dir = build_path
		com.Stdout = os.Stdout
		com.Stdin = os.Stdin
		com.Stderr = os.Stderr
		com.Run()
		fmt.Println("正在复制项目文件：", i)
		sthelp.CopyDir(filepath.Join(vue_path, i, "dist"), filepath.Join(os_dir, "data", "static"))
		sthelp.CopyDir(filepath.Join(vue_path, i, "dist"), static_dir)
	}

	//admin端
	{
		build_path := filepath.Join(vue_path, "admin")
		com2 := exec.Command("npm", "i")
		com2.Dir = build_path
		com2.Stdout = os.Stdout
		com2.Stdin = os.Stdin
		com2.Stderr = os.Stderr
		com2.Run()
		com := exec.Command("npm", "run", "build")
		com.Dir = build_path
		com.Stdout = os.Stdout
		com.Stdin = os.Stdin
		com.Stderr = os.Stderr
		com.Run()
		fmt.Println("正在复制项目文件：", "admin")
		sthelp.CopyDir(filepath.Join(vue_path, "admin", "dist"), filepath.Join(os_dir, "data", "admin"))
		sthelp.CopyDir(filepath.Join(vue_path, "admin", "dist"), admin_dir)
	}

	//index端
	{
		build_path := filepath.Join(vue_path, "index")
		com2 := exec.Command("npm", "i")
		com2.Dir = build_path
		com2.Stdout = os.Stdout
		com2.Stdin = os.Stdin
		com2.Stderr = os.Stderr
		com2.Run()
		com := exec.Command("npm", "run", "build")
		com.Dir = build_path
		com.Stdout = os.Stdout
		com.Stdin = os.Stdin
		com.Stderr = os.Stderr
		com.Run()
		fmt.Println("正在复制项目文件：", "index")
		sthelp.CopyDir(filepath.Join(vue_path, "index", "dist"), filepath.Join(os_dir, "data", "index"))
		sthelp.CopyDir(filepath.Join(vue_path, "index", "dist"), index_dir)
	}

	//编译go项目
	go_path := os_dir
	{
		com := exec.Command("go", "build")
		com.Dir = go_path
		com.Stdout = os.Stdout
		com.Stdin = os.Stdin
		com.Stderr = os.Stderr
		com.Run()
		if sthelp.PathExists(filepath.Join(go_path, "stforum.exe")) {
			copyFile(filepath.Join(go_path, "stforum.exe"), filepath.Join(bin_dir, "stforum.exe"))
		} else {
			copyFile(filepath.Join(go_path, "stforum"), filepath.Join(bin_dir, "stforum"))
		}
	}
	timenow := time.Now().Format("20060102150405")
	sthelp.ToZip("./bin", filepath.Join(os_dir, "./stforum_"+timenow+".zip"))
}
